
#region Using Statements
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using PacManPP.Managers;
#endregion

namespace GameStateManagement
{
  
    class GameplayScreen : GameScreen
    {
       
        ContentManager content;
        SpriteFont gameFont;
        Boolean dontPlay = false;
        GameManager gameManager;

        
      
        public GameplayScreen()
        {
            TransitionOnTime = TimeSpan.FromSeconds(1.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
            gameManager = new GameManager();
        }     
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(ScreenManager.Game.Services, "Content");

            gameFont = content.Load<SpriteFont>("gamefont");
            gameManager.SpriteBatch = ScreenManager.SpriteBatch;
            gameManager.Graphics = ScreenManager.GraphicsDevice;
            gameManager.Content = content;
            gameManager.LoadContent();
            gameManager.gameFont = gameFont;

            Thread.Sleep(1000);

          
            ScreenManager.Game.ResetElapsedTime();
            SoundManager.contentManager = content;
            SoundManager.LoadContent();
            SoundManager.StartSound();
        }


     
        public override void UnloadContent()
        {
            content.Unload();
        }


    
    
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);


            if (GameManager.dots_amount == 0 || GameManager.lives == 0)
            {
                dontPlay = true;
                if (GameManager.lives == 0)
                    ScreenManager.AddScreen(new MessageBoxScreen("You lose !!! "), null);
                else
                    ScreenManager.AddScreen(new MessageBoxScreen("You win !!! \n You have " + GameManager.points + " points" ), null);
            }
            else
            {
                gameManager.Update(gameTime);
            }
        }


    
        public override void HandleInput(InputState input)
        {
            
            int playerIndex = (int)ControllingPlayer.Value;
            KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
            
            gameManager.HandleInput(keyboardState);
        }



        public override void Draw(GameTime gameTime)
        {
            
            ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
                                               Color.Black, 0, 0);
         
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            spriteBatch.Begin();
            if(dontPlay == false )
                gameManager.Draw(gameTime);          
            spriteBatch.End();

            if (TransitionPosition > 0)
                ScreenManager.FadeBackBufferToBlack((int)(1f - TransitionAlpha));
        }


    }
}
